package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.usc.file.UploadFiles;

import com.bean.Homework;
import com.bean.HomeworkPath;
import com.bean.Student;

public class HomeworkDao extends DaoUtil{
	private static final boolean Flase = false;
	private PreparedStatement stat = null;
	private Connection conn = null;
	private ResultSet rs = null; 
	
	//添加作业
	public boolean addHomework(Homework homework){
		String sql = "insert into homework(homeworkName,homeworkContent,courseName,homepath,belongTea) values (?,?,?,?,?);";
		String workpath = UUID.randomUUID().toString(); //系统自动生成目录，该目录唯一
		conn = this.getConnection();
		try {
			stat = conn.prepareStatement(sql);
			stat.setString(1, homework.getHomeworkName());
			stat.setString(2, homework.getHomeworkContent());
			stat.setString(3, homework.getCourseName());
			stat.setString(4, workpath);//只要新建作业，就会有系统自动生成目录，该目录唯一。
			stat.setInt(5, homework.getBelongTea());
			return stat.execute();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	//得到作业列表
	public List<Homework> getHomeworkList(){
		List<Homework> homeworkList = new ArrayList<Homework>();
		String sql = "select * from homework";
		//String sql2 = "select score from homeworkpath where homepath=?";
		conn = this.getConnection();
		try {
			stat = conn.prepareStatement(sql);
			rs = stat.executeQuery();
			while(rs.next()){
				Homework homework = new Homework();
				homework.setHomeworkName(rs.getString("homeworkName"));
				homework.setCourseName(rs.getString("courseName"));
				homework.setHomeworkContent(rs.getString("homeworkContent"));
				homework.setHomePath(rs.getString("homepath"));
				homework.setBelongTea(rs.getInt("belongTea"));
				System.out.println(rs.getInt("belongTea"));
				homeworkList.add(homework);
			}
			return homeworkList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}
	
	public List<UploadFiles> getUploadFileslist(String u){
		List<UploadFiles> uploadFiles = new ArrayList<UploadFiles>();
		//模糊查询homepath表中含有教师创建时生成的字符串，以便定位作业
		String sql = "select * from homeworkpath where realpath like "+"'%"+u+"%'";
		System.out.println(sql);
		conn = this.getConnection();
		try {
			stat = conn.prepareStatement(sql);
			rs = stat.executeQuery();
			while(rs.next()){
				UploadFiles upfiles = new UploadFiles();
				upfiles.setUploadFileName(rs.getString(5));
				upfiles.setUploadRealName(rs.getString(6));
				upfiles.setScore(rs.getInt(7));
				upfiles.setStuUserID(rs.getInt(2));
				//upfiles.setHomeworkName(rs.getString(3));
				//upfiles.setHomeworkContent(rs.getString(4));
				uploadFiles.add(upfiles);
			}
			return uploadFiles;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}	
	
	
	
	public void saveWorkPath(HomeworkPath homeinfo){
		String sql = "insert into homeworkpath(stuUserID,fileName,realName,realpath,homeworkName,homeworkContent) values (?,?,?,?,?,?);";;
		conn = this.getConnection();
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1, homeinfo.getStuUserID());
			stat.setString(2, homeinfo.getFileName());
			stat.setString(3, homeinfo.getRealName());
			stat.setString(4, homeinfo.getRealpath());
			System.out.println("准备加载");
			System.out.println(homeinfo.getHomeworkName());
			stat.setString(5, homeinfo.getHomeworkName());
			stat.setString(6, homeinfo.getHomeworkContent());
			
			stat.execute();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
	}
	

	//获得教师作业信息
	public Homework getteahomework(String homepath){
		conn = this.getConnection();
		String sql = "select * from homework where homepath = ? ";
		try {
			Homework home = new Homework();
			stat = conn.prepareStatement(sql);			
			stat.setString(1, homepath);
			rs = stat.executeQuery();
			if(rs.next()){
				home.setHomeworkName(rs.getString(2));
				home.setHomeworkContent(rs.getString(4));
				System.out.println();
				System.out.println("参数2"+rs.getString(2));
				System.out.println(rs.getString(4));
			}
			return home;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			closed();
		}
	
		
	}
	
	
	public boolean setscore(int scores, String name) {
		conn = this.getConnection();
		String sql = "UPDATE homeworkpath SET score = ? WHERE realName = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1,scores);
			stat.setString(2, name);
			stat.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			return Flase;
		}finally {
			closed();
		}
	}
	
	
	public List<HomeworkPath> homeworkPathList(){
		List<HomeworkPath> homeworkPathList = new ArrayList<HomeworkPath>();
		Student stu = new Student();
		stu = (Student) ServletActionContext.getRequest().getSession().getAttribute("stu");
		
		conn = this.getConnection();
		
		String sql = "select * from homeworkpath where stuUserID = "+stu.getStuUserID();
		
		try {
			stat = conn.prepareStatement(sql);
			rs = stat.executeQuery();
			while(rs.next()){
				HomeworkPath homeworkPath = new HomeworkPath();
				//System.out.println(rs.getString("score"));
				homeworkPath.setScore(rs.getString("score"));
				homeworkPathList.add(homeworkPath);
				
			}
			return homeworkPathList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return null;
		
	}
	
}
